//
//  luoerNewFeatureCell.swift
//  WeiBoStudy
//
//  Created by luoerliu on 16/5/17.
//  Copyright © 2016年 刘文平. All rights reserved.
//

import UIKit
class luoerNewFeatureCell: UICollectionViewCell {
//    定义一个cell的索引
    var index: Int = 0
    {
        didSet
        {
//          根据cell的索引为其设置背景图片
               bkgimageView.image = UIImage(named: "new_feature_\(index + 1)")
//            隐藏开始按钮
            startButton.hidden = true
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override init(frame: CGRect)
    {
        super.init(frame:frame)
        prepareUI()
    }
    private func prepareUI()
    {
         contentView.addSubview(bkgimageView)
        contentView.addSubview(startButton)
//        MARK:----为cell中的控件添加约束
//        设置背景图片的约束
        bkgimageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(self.contentView)
//            设置按钮的约束
            startButton.snp_makeConstraints { (make) -> Void in
                make.centerX.equalTo(self.contentView)
                make.bottom.equalTo(self.contentView).offset(-160)
            }
        }
    }
//    设置开始按钮的动画
    func startAnimation()
    {
//        动画开始之前让按钮显示
        startButton.hidden = false
        // 在动画前设置transform.scale为0让按钮变成最小的状态
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        UIView.animateWithDuration(0.5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            // 恢复到原来的大小
            self.startButton.transform = CGAffineTransformMakeScale(1, 1) // 清空transform到默认的状态 scale = 1
            }, completion: nil)
    }
//    按钮的点击事件实现
    func startButtonDidClick() {
        // 切换到CZMainTabBarController
        let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        appDelegate.switchRootViewController(luoerTabbarController())
    }
//    MARK:----懒加载cell里的控件
//    背景图片
    private lazy var bkgimageView:UIImageView = UIImageView(image: UIImage(named: "new_feature_1"))
//    /// 开始按钮
    private lazy var startButton: UIButton = {
        let button = UIButton()
        // 设置文字
        button.setTitle("开始体验", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
//        按钮默认隐藏
        button.hidden = true
//        按钮的点击事件
        button.addTarget(self, action: "startButtonDidClick", forControlEvents: UIControlEvents.TouchUpInside)
        return button
    }()

}
